//
// Copyright (c) 2009-2011, chunquedong
//
// This file is part of ChunMap project
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
//
// History:
//   2011-05-03  Jed Young  Creation
//

using chunmapModel
using slanData

**
** FeatureSet
**
@Js
@Serializable { simple = true }
abstract class FeatureSet : DataSource
{
  abstract This add(Feature f)
  abstract Feature? remove(Feature f)

//////////////////////////////////////////////////////////////////////////
// Field
//////////////////////////////////////////////////////////////////////////

  override Envelope envelope
  Obj? dataResources
  override Metadata metadata
  override Schema schema

  Uri? uri
  [Str:Str]? options

  new make(|This| f) { f(this) }

  Void reComputeEnvelope()
  {
    eb := EnvelopeBuilder.makeNone
    each(Condition.empty) |Feature f|
    {
      eb.merge(f.envelope)
    }
    envelope = eb.toEnvelope
  }

//////////////////////////////////////////////////////////////////////////
// Serialization
//////////////////////////////////////////////////////////////////////////

  override Str toStr()
  {
    str := uri.toStr
    if (options == null) return str
    options.each |v,k|
    {
      str += ",$k:$v"
    }
    return str
  }

  static FeatureSet fromStr(Str s)
  {
    t := s.split(',')
    FeatureSet? fc
    if (t.size > 1)
    {
      options := [Str:Str][:]
      for (i:=1; i< t.size; ++i)
      {
        p := t[i].split(':')
        if (p.size > 1)
          options[p[0]] = p[1]
      }
      fc = GeoDataAdapter.buildFeatureSet(t[0].toUri, options)
    }
    else
      fc = GeoDataAdapter.buildFeatureSet(t[0].toUri)
    return fc
  }
}